<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="250px">
        <router-link to="/">
          <div class="title-bar">
            <img src="../assets/logo.png" alt="logo" class="logo" />
            <div class="title">消防器材管理系统</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
        />
      </a-col>
      <!-- 用户信息展示栏 -->
      <a-col flex="140px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown>
              <a-space>
                <a-avatar :src="loginUserStore.loginUser.avatar" />
                {{ loginUserStore.loginUser.nickName ?? '无名' }}
              </a-space>
              <template #overlay>
                <a-menu>
                  <a-menu-item>
                    <UserOutlined />
                    <a-button type="link" :href="`/user/add_user?id=${loginUserStore.loginUser.id}`">个人信息</a-button>
                  </a-menu-item>
                  <a-menu-item @click="doLogout">
                    <LogoutOutlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <div v-if="!list.includes($router.currentRoute.value.path)">
              <a-button type="primary" href="/user/login">登录</a-button>
            </div>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import { HomeOutlined, LogoutOutlined, UserOutlined } from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { userLogoutUsingPost } from '@/api/authUserController.ts'
import checkAccess from '@/access/checkAccess.ts'
import { ACCESS_ENUM } from '@/access/accessEnum.ts'

const loginUserStore = useLoginUserStore()
const list = [
  '/user/login', '/user/register'
]
// 菜单列表
const menus = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
    meta: {
      hideInMenu: false,
      access: null
    }
  },
  {
    key: '/admin/userManage',
    label: '用户管理',
    title: '用户管理',
    meta: {
      hideInMenu: false,
      access: ACCESS_ENUM.SYSTEM_ADMIN + ',' + ACCESS_ENUM.LEADER
    }
  },
  {
    key: 'equipment',
    label: '设备管理',
    title: '设备管理',
    meta: {
      hideInMenu: false,
      access: null
    },
    children: [
      {
        key: '/equipment/equipmentManage',
        label: '设备列表',
        title: '设备列表',
        meta: {
          hideInMenu: false,
          access: null
        }
      },
      {
        key: '/category/categoryManage',
        label: '分类管理',
        title: '分类管理',
        meta: {
          hideInMenu: false,
          access: null
        }
      },
      {
        key: '/label/labelManage',
        label: '标签管理',
        title: '标签管理',
        meta: {
          hideInMenu: false,
          access: null
        }
      }
    ]
  },
  {
    key: 'orders',
    label: '管理订单',
    title: '管理订单',
    meta: {
      hideInMenu: false,
      access: null
    },
    children: [
      {
        key: '/orders/ordersManage',
        label: '订单管理',
        title: '订单管理',
        meta: {
          hideInMenu: false,
          access: null
        },
      },
      {
        key: '/company/companyMange',
        label: '公司管理',
        title: '公司管理',
        meta: {
          hideInMenu: false,
          access: null
        }
      }
    ]
  },
  {
    key:'/fixEquipment/page',
    label: '维修设备',
    title: '维修设备',
    meta: {
      hideInMenu: false,
      access: null
    },
  }
]

const menuToRouteItem = (menu: any): any => {
  return {
    ...menu,
    meta: {
      hideInMenu: menu.meta.hideInMenu || false,
      access: menu.meta.access || null
    }
  }
}

// 过滤菜单项
const items = computed(() => {
  return menus.filter((menu) => {
    const item = menuToRouteItem(menu)
    if (item.meta?.hideInMenu) {
      return false
    }
    // 只有当 meta.access 存在时才进行权限检查
    if (item.meta?.access) {
      return checkAccess(loginUserStore.loginUser, item.meta.access as string)
    }
    // 如果 meta.access 不存在，则默认允许显示
    return true
  })
})

const router = useRouter()
console.log(router.currentRoute.value.path)
// 当前要高亮的菜单项
const current = ref<string[]>([])
// 监听路由变化，更新高亮菜单项
router.afterEach((to, from, next) => {
  current.value = [to.path]
})
// 路由跳转事件
const doMenuClick = ({ key }) => {
  router.push({
    path: key
  })
}

const doLogout = async () => {
  const res = await userLogoutUsingPost()
  if (res.data.code === 0) {
    loginUserStore.setLoginUser({
      userName: '未登录'
    })
    message.success('退出登录成功')
    await router.push('/user/login')
  } else {
    await router.push('/user/login')
    // message.error('退出登录失败')
  }
}
</script>

<style scoped>
#globalHeader .title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

.logo {
  height: 48px;
}

/* 添加子菜单样式 */
:deep(.ant-menu-submenu-title) {
  padding: 0 20px !important;
}

:deep(.ant-menu-submenu-popup) {
  min-width: 160px;
}

:deep(.ant-menu-item) {
  padding-left: 32px !important;
}
</style>
